package com.example.demo.controller;

import com.example.demo.common.Result;
import com.example.demo.entity.Member;
import com.example.demo.service.InventoryService;
import com.example.demo.service.SaleOrderService;
import com.example.demo.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 仪表板控制器
 * 提供系统概览和统计数据
 */
@RestController
@RequestMapping("/api/dashboard")
@CrossOrigin(origins = "*")
public class DashboardController {
    
    @Autowired
    private InventoryService inventoryService;
    
    @Autowired
    private SaleOrderService saleOrderService;
    
    @Autowired
    private MemberService memberService;
    
    /**
     * 获取系统概览数据
     */
    @GetMapping("/overview")
    @PreAuthorize("hasRole('ADMIN') or hasRole('MANAGER')")
    public Result<Map<String, Object>> getOverview() {
        Map<String, Object> overview = new HashMap<>();
        
        // 库存统计
        overview.put("lowStockItems", inventoryService.findLowStockItems().size());
        overview.put("outOfStockItems", inventoryService.findOutOfStockItems().size());
        overview.put("overstockItems", inventoryService.findOverstockItems().size());
        
        // 今日销售统计
        LocalDateTime startOfDay = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0);
        LocalDateTime endOfDay = LocalDateTime.now().withHour(23).withMinute(59).withSecond(59);
        Double todaySales = saleOrderService.getTotalSalesByDateRange(startOfDay, endOfDay);
        overview.put("todaySales", todaySales != null ? todaySales : 0.0);
        
        // 会员统计
        overview.put("totalMembers", memberService.findAll().size());
        overview.put("activeMembers", memberService.findByStatus(Member.MemberStatus.ACTIVE).size());
        
        return Result.success(overview);
    }
    
    /**
     * 获取库存预警
     */
    @GetMapping("/inventory-alerts")
    @PreAuthorize("hasRole('ADMIN') or hasRole('MANAGER') or hasRole('STOCK_KEEPER')")
    public Result<Map<String, Object>> getInventoryAlerts() {
        Map<String, Object> alerts = new HashMap<>();
        
        alerts.put("lowStock", inventoryService.findLowStockItems());
        alerts.put("outOfStock", inventoryService.findOutOfStockItems());
        alerts.put("overstock", inventoryService.findOverstockItems());
        
        return Result.success(alerts);
    }
    
    /**
     * 获取销售统计
     */
    @GetMapping("/sales-stats")
    @PreAuthorize("hasRole('ADMIN') or hasRole('MANAGER')")
    public Result<Map<String, Object>> getSalesStats(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        
        Map<String, Object> stats = new HashMap<>();
        
        // 这里可以根据日期范围查询销售数据
        // 简化处理，返回今日数据
        LocalDateTime startOfDay = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0);
        LocalDateTime endOfDay = LocalDateTime.now().withHour(23).withMinute(59).withSecond(59);
        
        Double totalSales = saleOrderService.getTotalSalesByDateRange(startOfDay, endOfDay);
        stats.put("totalSales", totalSales != null ? totalSales : 0.0);
        stats.put("orderCount", saleOrderService.findByOrderTimeBetween(startOfDay, endOfDay).size());
        
        return Result.success(stats);
    }
}
